﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace YJTTool.Common.Manager
{
    public static class WindowManager
    {
        private static Hashtable _RegisterWindow = new Hashtable();

        public static void Register<T>(string key)
        {
            if (!_RegisterWindow.Contains(key))
            {
                _RegisterWindow.Add(key, typeof(T));
            }
        }

        public static void Register(string key, Type t)
        {
            if (!_RegisterWindow.Contains(key))
            {
                _RegisterWindow.Add(key, t);
            }
        }

        public static void Remove(string key)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                _RegisterWindow.Remove(key);
            }
        }

        public static void Show(string key, object VM)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                Type windowType = (Type)_RegisterWindow[key];
                var windowInstance = Activator.CreateInstance(windowType) as Window; // 尝试转换为 Window 类型

                if (windowInstance != null)
                {
                    windowInstance.DataContext = VM;
                    windowInstance.Show();
                }
                else
                {
                    // 处理类型不匹配的情况
                    throw new InvalidCastException($"The type {windowType} is not a subclass of Window.");
                }
            }
        }
    }
}
